package com.ruoyi.retail.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.retail.domain.SellGood;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.retail.domain.Sell;
import com.ruoyi.retail.service.ISellService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 零售管理Controller
 *
 * @author ruoyi
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/sell/sell")
public class SellController extends BaseController
{
    @Autowired
    private ISellService sellService;

    /**
     * 查询零售管理列表
     */
    @PreAuthorize("@ss.hasPermi('sell:sell:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sell sell)
    {
        startPage();
        List<Sell> list = sellService.selectSellList(sell);
        return getDataTable(list);
    }

    /**
     * 导出零售管理列表
     */
    @PreAuthorize("@ss.hasPermi('sell:sell:export')")
    @Log(title = "零售管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sell sell)
    {
        List<Sell> list = sellService.selectSellList(sell);
        ExcelUtil<Sell> util = new ExcelUtil<Sell>(Sell.class);
        util.exportExcel(response, list, "零售管理数据");
    }

    /**
     * 获取零售管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('sell:sell:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(sellService.selectSellById(id));
    }

    /**
     * 新增零售管理
     */
    @PreAuthorize("@ss.hasPermi('sell:sell:add')")
    @Log(title = "零售管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sell sell)
    {
        if (sell.getProductList() != null) {
            BigDecimal total = sell.getProductList().stream()
                    .map(SellGood::getAmount)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            sell.setTotalPrice(total);
            sell.setPayPrice(total); // 或计算折扣等
        }
        return toAjax(sellService.insertSell(sell));
    }

    /**
     * 修改零售管理
     */
    @PreAuthorize("@ss.hasPermi('sell:sell:edit')")
    @Log(title = "零售管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult edit(@RequestBody Sell sell)
    {
        return toAjax(sellService.updateSell(sell));
    }


    @PreAuthorize("@ss.hasPermi('sell:sell:edit')")
    @Log(title = "零售管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePaymentStatus")
    public AjaxResult updatePaymentStatus(@RequestBody Map<String, Object> params) {
        Sell sell = new Sell();
        sell.setId(Integer.parseInt(params.get("id").toString()));
        sell.setPaymentStatus(params.get("paymentStatus").toString());
        return toAjax(sellService.updateSellPaymentStatus(sell));
    }
    /**
     * 删除零售管理
     */
    @PreAuthorize("@ss.hasPermi('sell:sell:remove')")
    @Log(title = "零售管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sellService.deleteSellByIds(ids));
    }

    @GetMapping("/product/list")
    public TableDataInfo listProduct() {
        // 这里调用商品服务的查询方法
        startPage();
        List<SellGood> list = sellService.selectProductList(new SellGood());
        return getDataTable(list);
    }
}
